command line 您所在的位置:网站首页 how to use sed to find and replace string in command line

command line

2023-07-29 14:11| 来源: 网络整理| 查看: 265

In circumstances where the replacement string or pattern string contain slashes, you can make use of the fact that GNU sed allows an alternative delimiter for the substitute command. Common choices for the delimiter are the pipe character | or the hash # - the best choice of delimiting character will often depend on the type of file being processed. In your case you can try

sed -i "s#_HOME_DIR_#$HOME_DIR#"

Also note that you mistyped your variable name $HOME_DIR (it does not start with an underscore).

$ HOME_DIR=/opt/my_home $ echo "_HOME_DIR_/config/data/_DOMAIN_/users.conf" | sed "s#_HOME_DIR_#$HOME_DIR#" /opt/my_home/config/data/_DOMAIN_/users.conf

Just fyi you may want to avoid using all-caps for user variables in your script - they should be reserved for system variables. Also the g switch is not necessary unless you want to make multiple replacements on a single line.



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有